package com.bjpowernode.crm.workbench.service.serviceimpl;

import com.bjpowernode.crm.setting.dao.UserDao;
import com.bjpowernode.crm.vo.PageinationVO;
import com.bjpowernode.crm.workbench.dao.ActivityDao;
import com.bjpowernode.crm.workbench.dao.RemarkDao;
import com.bjpowernode.crm.workbench.eneity.Activity;
import com.bjpowernode.crm.workbench.exception.ActivityException;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityServiceImpl implements ActivityService {
    private ActivityDao activityDao;
    private RemarkDao remarkDao;
    private UserDao userDao;
    public void setActivityDao(ActivityDao activityDao) {
        this.activityDao = activityDao;
    }
    public void setRemarkDao(RemarkDao remarkDao) {this.remarkDao=remarkDao;}
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    @Transactional
    //进行信息添加操作
    public void add(Activity activity) throws ActivityException {
        //执行添加操作
        int result=activityDao.add(activity);
        if (result!=1){
            throw new ActivityException("添加失败请检查");
        }
    }

    @Override
    @Transactional
    public PageinationVO<Activity> queryList(Map<String, Object> map, Integer pageNo, Integer pageSize){
        //查询出否和条件的记录条数
        int title=activityDao.getTitle(map);
        //先进行分页
        PageHelper.startPage(pageNo,pageSize);
        List<Activity> activityList=activityDao.queryList(map);
        PageinationVO<Activity> pageinationVO=new PageinationVO<>();
        pageinationVO.setTitle(title);
        pageinationVO.setDataList(activityList);
        return pageinationVO;
    }

    @Override
    @Transactional
    public boolean delete(String[] params) {
        boolean flag=true;
        //查询需要删除的备注数量
        int count=remarkDao.selectByActivityIds(params);
        //实际删除的数量
        int result=remarkDao.deleteByActivityIds(params);
        if (count!=result){
            flag=false;
        }
        //删除市场活动
        int countRes=activityDao.delete(params);
        if (countRes!=params.length){
            flag=false;
        }
        return flag;
    }

    @Override
    @Transactional
    public Activity selectById(String id) {
        //调用dao进行查询
        Activity activity=activityDao.selectById(id);
        return activity;
    }

    @Override
    @Transactional
    public Boolean updateById(Activity activity) {
        Boolean flag=true;
        //调用dao
        int result=activityDao.updateById(activity);
        if (result!=1){
            flag=false;
        }
        return flag;
    }

    @Override
    @Transactional
    public Map<String,Object> queryById(String id) {
        //先查询activity信息
        Activity activity=activityDao.selectById(id);
        //再查询用户名字
        String username=userDao.queryById(activity.getOwner());
        //将市场信息和用户名字写入map
        Map<String,Object> map=new HashMap<>();
        map.put("activity",activity);
        map.put("username",username);
        return map;
    }

    @Override
    public List<Activity> getAllActivity(String clueId) {
        //调用dao
        List<Activity> activityList=activityDao.getAllActivity(clueId);
        return activityList;
    }

    @Override
    public List<Activity> getAllActivityByIdAndName(Map<String, String> paramMap) {
        //调用dao
        List<Activity> activityList=activityDao.getAllActivityByIdAndName(paramMap);
        return activityList;
    }

    @Override
    public List<Activity> getActivityList() {
        //调用dao
        List<Activity> activityList=activityDao.getActivityList();
        return activityList;
    }

    @Override
    public List<Activity> getActivityByName(String name) {
        List<Activity> activityList=activityDao.getActivityByName(name);
        return activityList;
    }
}
